<template>
  <div>
    <basic-container>
      <avue-form :option="option" ref="userInfoForm" v-model="form" @tab-click="handleTabClick" @submit="handleSubmit"> </avue-form>
    </basic-container>
  </div>
</template>

<script>
// import option from "@/option/user/info";
import { optionConfig } from '@/option/user/info';
import { getUserInfo, getTemplateInfo, getEquireMentInfo, updateInfo, updatePassword } from '@/api/system/user';
import md5 from 'js-md5';
import func from '@/util/func';
import CommonModalTemplate from '@/components/common-modal-template/common-modal-template.vue';

export default {
  components: {
    // CommonPrintersTemplate,
    CommonModalTemplate
  },
  data() {
    return {
      index: 0,
      option: optionConfig(this),
      model: {},
      form: {},
      modalName: '',
      tableOption: {}, //公用模态框配置信息
      printInfo: {}, //打印机打印模版信息
      timer: new Date().getTime(),
      echoPrinterName: '',
      echoLabelTemplateName: ''
    };
  },
  created() {
    this.handleWitch();
  },
  mounted() {},
  methods: {
    handleSubmit(form, done) {
      if (this.index === 0) {
        updateInfo(form).then(
          (res) => {
            if (res.data.success) {
              this.$message({
                type: 'success',
                message: '修改信息成功!'
              });
            } else {
              this.$message({
                type: 'error',
                message: res.data.msg
              });
            }
            done();
          },
          (error) => {
            window.console.log(error);
            done();
          }
        );
      } else {
        updatePassword(md5(form.oldPassword), md5(form.newPassword), md5(form.newPassword1)).then(
          (res) => {
            if (res.data.success) {
              this.form.oldPassword = '';
              this.form.newPassword = '';
              this.form.newPassword1 = '';
              this.$message({
                type: 'success',
                message: '修改密码成功!'
              });
            } else {
              this.$message({
                type: 'error',
                message: res.data.msg
              });
            }
            done();
          },
          (error) => {
            window.console.log(error);
            done();
          }
        );
      }
    },
    resetData() {
      this.form = {};
      console.log('restatFatdata');
    },
    handleWitch() {
      if (this.index === 0) {
        getUserInfo().then((res) => {
          const user = res.data.data;
          this.form = {
            id: user.id,
            avatar: user.avatar,
            name: user.name,
            realName: user.realName,
            phone: user.phone,
            email: user.email,
            defaultSiteId: user.defaultSiteId,
            printerName: this.echoPrinterName
            // labelTemplateName: this.echoLabelTemplateName,
          };
        });
      }
    },
    handleTabClick(tabs) {
      this.index = func.toInt(tabs.index);
      this.handleWitch();
    }
  }
};
</script>

<style></style>
